from flask import Flask
import os

app = Flask(__name__)
profile = os.environ.get('MICHELANGELO_PROFILE', '')
app.config.from_object('config.' + profile + 'Config')


from michelangelo.database import db_session
from michelangelo.views import general
import logging
from logging import Formatter
from logging.handlers import TimedRotatingFileHandler

@app.teardown_request
def remove_db_session(exception):
  db_session.remove()

def init_logger():
  logger = logging.getLogger('Michelangelo')
  handler = TimedRotatingFileHandler(app.config['LOG_FILE'], when="midnight")
  handler.suffix = '%Y-%m-%d'
  handler.setFormatter(Formatter(
    '%(asctime)s %(module)s %(levelname)s: %(message)s '
    '[in %(pathname)s:%(lineno)d]'
  ))
  app.logger.addHandler(handler)
  app.logger.setLevel(logging.INFO)
  logger.addHandler(handler)
  logger.setLevel(logging.INFO)

init_logger()
app.logger.info('Michelangelo is using profile: [{0}]'.format(app.config['PROFILE']))


app.register_blueprint(general.mod)

